<template>
  <div class="employee-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <div class="search-label">车牌号码</div>
      <el-input v-model="params.carNumber" clearable placeholder="请输入车牌号码" class="search-main" @clear="clearSearch" />
      <div class="search-label">车主姓名</div>
      <el-input v-model="params.personName" clearable placeholder="请输入车主姓名" class="search-main" @clear="clearSearch" />
      <div>状态</div>
      <el-select v-model="params.cardStatus" clearable placeholder="请选择" @clear="clearSearch">
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
      <el-button type="primary" @click="monthList">查询</el-button>
    </div>
    <!-- 添加 -->
    <div class="create-container">
      <el-button type="primary" @click="$router.push('/monthAdd')">添加月卡</el-button>
      <el-button type="primary">批量删除</el-button>
    </div>
    <!-- 表格 -->
    <div class="table">
      <el-table
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="id" label="序号" width="50" />
        <el-table-column prop="personName" label="车主名称" />
        <el-table-column prop="phoneNumber" label="联系方式" width="120" />
        <el-table-column prop="carNumber" label="车牌号码" width="120" />
        <el-table-column prop="carBrand" label="车辆品牌" width="120" />
        <el-table-column prop="totalEffectiveDate" label="剩余有效天数" width="120" />
        <el-table-column prop="cardStatus" label="状态" width="120">
          <template #default="scope">
            {{ formartStatus(scope.row.cardStatus) }}
          </template>
        </el-table-column>
        <el-table-column prop="name" label="操作" width="190">
          <template #default="scope">
            <el-button size="mini" type="text">续费</el-button>
            <el-button size="mini" type="text">查看</el-button>
            <el-button size="mini" type="text" @click="edit(scope.row.id)">编辑</el-button>
            <el-button size="mini" type="text" @click="del(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 底部分页 -->
    <div class="page-container">
      <el-pagination
        layout="total, prev, pager, next"
        :total="total"
        @current-page="pageChange"
      />
    </div>
  </div>
</template>

<script>
import { getMonthAPI, delimitersAPI } from '@/api/month'
export default {
  name: 'RodManage',
  data() {
    return {
      tableData: [], // 数据列表
      multipleSelection: [],
      value: '',
      total: 0,
      params: {
        page: 1,
        pageSize: 10,
        carNumber: null,
        personName: '',
        cardStatus: ''// 状态
      },
      // 状态
      options: [
        {
          value: '0',
          label: '可用'
        },
        {
          value: '1',
          label: '已过期'
        }
      ]
    }
  },
  created() {
    this.monthList()
  },
  methods: {
    clearSearch() {
      this.monthList()
      this.params.page = 1
    },
    // 用于保存选中的行
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    toggleSelection(rows) {
      if (rows) {
        rows.forEach(row => {
          console.log(123)
          this.$refs.multipleTable.toggleRowSelection(row)
        })
      } else {
        this.$refs.multipleTable.clearSelection()
      }
    },
    // 状态
    formartStatus(type) {
      const TYPEMAP = {
        0: '可用',
        1: '已过期'
      }
      return TYPEMAP[type]
    },
    // 页面渲染
    async monthList() {
      const res = await getMonthAPI(this.params)
      this.tableData = res.data.rows
      this.total = res.data.total
    },
    // 分页
    pageChange(page) {
      this.params.page = page
      this.monthList()
    },
    // 点击编辑
    edit(id) {
      // const res = await editmonthAPI(id)
      this.$router.push({
        path: '/monthAdd',
        query: { id }
      })
      console.log(id)
    },
    del(id) {
      this.$confirm('确认删除吗？', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        console.log(id)
        await delimitersAPI(id)
        this.$message.success('删除成功')
        this.monthList()
      })
    }
  }
}
</script>
<style scoped lang="scss">
.employee-container {
  padding: 10px;
  background-color: #f4f6f8;
  height: 100vh;
  .table {
    background-color: #f4f6f8;
    height: 100vh;
  }
  .search-container {
    display: flex;
    align-items: center;
    border-bottom: 1px solid rgb(237, 237, 237, 0.9);
    padding-bottom: 20px;

    .search-label {
      width: 100px;
    }

    .search-main {
      width: 220px;
      margin-right: 10px;
    }
  }
  .create-container {
    margin: 10px 0px;
  }
  .page-container {
    padding: 4px 0px;
    text-align: right;
  }
  .form-container {
    padding: 0px 80px;
  }
}
</style>
